package cn.explink.dop.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sun.star.lib.connections.socket.socketAcceptor;

import cn.explink.dop.entity.vo.AreaVO;
import cn.explink.dop.entity.vo.DeliveryManVO;
import cn.explink.dop.entity.vo.StorefrontVO;
import cn.explink.dop.service.AreaService;
import cn.explink.dop.service.DeliveryManService;
import cn.explink.dop.service.StorefrontService;
import cn.explink.eap.core.common.model.json.AjaxJson;

@Controller
@RequestMapping("/storefrontRelateController")
public class StorefrontRelateController {

	@Autowired
	private DeliveryManService deliveryManService;

	@Autowired
	private AreaService areaService;

	@Autowired
	private StorefrontService storefrontService;

	@RequestMapping(params = "index")
	public ModelAndView storefrontIndex(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("dop/storefront/storefrontrelate");
		return view;
	}

	/** 
	* @Title: assignAreaAndDeliveryman 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @author 小强
	* @param @param id
	* @param @param areaIdList
	* @param @param deliverymanIdList
	* @param @param request
	* @param @return    设定文件 
	* @return AjaxJson    返回类型 
	* @throws 
	*/
	@RequestMapping(params = "assignAreaAndDeliveryman")
	@ResponseBody
	public AjaxJson assignAreaAndDeliveryman(String id, @RequestParam(value = "areaIdList[]", required = false) List<String> areaIdList,
			@RequestParam(value = "deliverymanIdList[]", required = false) List<String> deliverymanIdList, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			// 获取店面
			StorefrontVO storefrontVO = this.storefrontService.queryById(id);
			// 分配区域
			List<AreaVO> areaVOList = new ArrayList<AreaVO>();
			if (null != areaIdList) {
				for (String areaId : areaIdList) {
					AreaVO areaVO = this.areaService.queryById(areaId);
					areaVOList.add(areaVO);
				}
				storefrontVO.setAreaList(areaVOList);
			}
			else {
				storefrontVO.setAreaList(null);
			}

			// 分配配送员
			List<DeliveryManVO> deliveryManVOList = new ArrayList<DeliveryManVO>();
			if (null != deliverymanIdList) {
				for (String deliverymanId : deliverymanIdList) {
					DeliveryManVO deliveryManVO = this.deliveryManService
							.queryById(deliverymanId);
					deliveryManVOList.add(deliveryManVO);
				}
				storefrontVO.setDeliveryManList(deliveryManVOList);
			} else {
				storefrontVO.setDeliveryManList(null);
			}
			this.storefrontService.saveOrUpdate(storefrontVO);

			j.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j;
	}
}
